package com.ims.excel.wait;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class C09readAndWriteExcel {

    public static void main(String[] args) throws Exception {
        /**
         * 读取  工作簿
         */
        InputStream inp = new FileInputStream("测试工作簿.xls");
        POIFSFileSystem fs = new POIFSFileSystem(inp);
        Workbook wb = new HSSFWorkbook(fs);

        /**
         * 读取 sheet row cell 设置cell值
         */
        // 获取Sheet
        Sheet sheet = wb.getSheetAt(0);
        // 获取行
        Row row = sheet.getRow(0);
        // 获取单元格
        if (row == null) {
            row = sheet.createRow(0);
        }
        Cell cell = row.getCell(0);
        if (cell == null) {
            cell = row.createCell(0);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("测试单元格");
        /**
         * 写入  工作簿
         */
        FileOutputStream fileOut = new FileOutputStream("测试工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}
